import asyncio
import time

import numpy as np

import app.model_phl.utils as utils
import app.model_phl.utils_sql as utils_sql
import app.model_phl.utils_json as utils_json
import pandas as pd
import warnings

warnings.filterwarnings('ignore')


def cal_app_risk_list(app_df, risk_app_df):
    """
    app_risk_list 风险类App的列表
    :param app_df app数据
    :param risk_app_df: 高危App数据
    """
    # 统一列名packageName：PackageName
    if 'packageName' in app_df.columns:
        app_df.rename(columns={'packageName': 'PackageName'}, inplace=True)
    # 统一列名FirstInstallTime：firstInstallTime
    if 'FirstInstallTime' in app_df.columns:
        app_df.rename(columns={'FirstInstallTime': 'firstInstallTime'}, inplace=True)
    # 将datetime列转换为datetime类型
    app_df.firstInstallTime = pd.to_datetime(app_df.firstInstallTime)
    # 包含风险类App的列表
    return app_df.loc[(app_df.get('PackageName').isin(risk_app_df.get('app_id')))]


def cal_app_loan_list(app_df, loan_app_df):
    """
    app_loan_list 借款类App的列表
    :param app_df app数据
    :param loan_app_df: 贷款类App数据
    """
    # 统一列名packageName：PackageName
    if 'packageName' in app_df.columns:
        app_df.rename(columns={'packageName': 'PackageName'}, inplace=True)
    # 统一列名FirstInstallTime：firstInstallTime
    if 'FirstInstallTime' in app_df.columns:
        app_df.rename(columns={'FirstInstallTime': 'firstInstallTime'}, inplace=True)
    # 将datetime列转换为datetime类型
    app_df.firstInstallTime = pd.to_datetime(app_df.firstInstallTime)
    # 将datetime列转换为datetime类型
    loan_app_df.release_date = pd.to_datetime(loan_app_df.release_date)

    return pd.merge(app_df, loan_app_df, how='inner', left_on='PackageName', right_on='app_id')


def cal_app_loan_inst_cnt(app_loan_list_df):
    """
    app_loan_inst_cnt 借款类App的个数
    :param app_loan_list_df 贷款类App列表
    """
    return app_loan_list_df.get('PackageName').drop_duplicates().count()


def cal_app_inst_d3_cnt(app_df, apply_date):
    """
    app_inst_d3_cnt 3天内安装的APP个数
    :param app_df 安装了的app列表
    :param apply_date 订单申请时间
    """
    # 统一列名packageName：PackageName
    if 'packageName' in app_df.columns:
        app_df.rename(columns={'packageName': 'PackageName'}, inplace=True)
    # 统一列名FirstInstallTime：firstInstallTime
    if 'FirstInstallTime' in app_df.columns:
        app_df.rename(columns={'FirstInstallTime': 'firstInstallTime'}, inplace=True)
    # 将日期字符串转换为datetime对象
    # 计算3天前的时间
    start_time = apply_date - pd.Timedelta(days=3)
    # 将datetime列转换为datetime类型
    app_df.firstInstallTime = pd.to_datetime(app_df.firstInstallTime)
    # 筛选符合条件的App
    return app_df.loc[
        # 大于等于开始时间
        (app_df.get('firstInstallTime') >= start_time) &
        # 小于等于结束时间
        (app_df.get('firstInstallTime') <= apply_date)
        ].get('PackageName').count()


def cal_release_date_app(app_loan_list_df, start_date=None, end_date=None):
    """
    计算根据发布时间App数量
    :param app_loan_list_df 贷款类App列表
    :param start_date: 发布开始时间
    :param end_date: 发布结束时间
    """
    list_df = app_loan_list_df.copy()
    if start_date is not None:
        # 在开始时间之后发布
        list_df = list_df.loc[(list_df.get('release_date') >= pd.Timestamp(start_date))]
    if end_date is not None:
        # 在结束时间之前发布
        list_df = list_df.loc[(list_df.get('release_date') <= pd.Timestamp(end_date))]

    return list_df.get('app_id').count()


def cal_app_loan_release2mw_cnt(app_loan_list_df, apply_date):
    """
    app_loan_release2mw_cnt 发布2个月以内的借款类App的个数
    :param app_loan_list_df 贷款类App列表
    :param apply_date 订单申请时间
    """
    return cal_release_date_app(
        app_loan_list_df=app_loan_list_df,
        start_date=apply_date - pd.DateOffset(months=2),
        end_date=apply_date
    )


def cal_app_loan_release2m_rate(app_loan_release2mw_cnt, app_loan_inst_cnt):
    """
    app_loan_release2m_rate 发布2个月内的借款类App占总借贷App比例
    :param app_loan_release2mw_cnt 发布2个月以内的借款类App的个数
    :param app_loan_inst_cnt 贷款类App安装数量
    """
    return utils.cal_rate(app_loan_release2mw_cnt, app_loan_inst_cnt, decimals=2)


def cal_app_loan_release3mw_cnt(app_loan_list_df, apply_date):
    """
    app_loan_release3mw_cnt 发布3个月内的借款类App的个数
    :param app_loan_list_df 贷款类App列表
    :param apply_date 订单申请时间
    """
    return cal_release_date_app(
        app_loan_list_df=app_loan_list_df,
        start_date=apply_date - pd.DateOffset(months=3),
        end_date=apply_date
    )


def cal_app_loan_release3m_rate(app_loan_release3mw_cnt, app_loan_inst_cnt):
    """
    app_loan_release3m_rate 发布3个月内的借款类App占总借贷App比例
    :param app_loan_release3mw_cnt 发布3个月内的借款类App的个数
    :param app_loan_inst_cnt 贷款类App安装数量
    """
    return utils.cal_rate(app_loan_release3mw_cnt, app_loan_inst_cnt, decimals=2)


def cal_app_loan_release3mp_cnt(app_loan_list_df, apply_date):
    """
    app_loan_release3mp_cnt 发布3个月以上的借款类App的个数
    :param app_loan_list_df 贷款类App列表
    :param apply_date 订单申请时间
    """
    return cal_release_date_app(
        app_loan_list_df=app_loan_list_df,
        end_date=apply_date - pd.DateOffset(months=3)
    )


def cal_app_loan_release6mp_cnt(app_loan_list_df, apply_date):
    """
    app_loan_release6mp_cnt 发布6个月以上的借款类App的个数
    :param app_loan_list_df 贷款类App列表
    :param apply_date 订单申请时间
    """
    return cal_release_date_app(
        app_loan_list_df=app_loan_list_df,
        end_date=apply_date - pd.DateOffset(months=6)
    )


def cal_app_loan_release1yp_cnt(app_loan_list_df, apply_date):
    """
    app_loan_release1yp_cnt 发布1年以上的借款类App的个数
    :param app_loan_list_df 贷款类App列表
    :param apply_date 订单申请时间
    """
    return cal_release_date_app(
        app_loan_list_df=app_loan_list_df,
        end_date=apply_date - pd.DateOffset(months=12)
    )


def cal_app_loan_inst_10Wp_cnt(app_loan_list_df):
    """
    app_loan_inst_10Wp_cnt GP下载大于（含）10W的借款类App的个数
    :param app_loan_list_df 贷款类App列表
    """
    return app_loan_list_df.loc[(app_loan_list_df.get('installs') >= 100000)].get('app_id').count()


def cal_app_loan_inst_10Wp_rate(app_loan_inst_10Wp_cnt, app_loan_inst_cnt):
    """
    app_loan_inst_10Wp_rate GP下载大于（含）10W的借款类App的个数占总借贷App比例
    :param app_loan_inst_10Wp_cnt GP下载大于（含）10W的借款类App的个数
    :param app_loan_inst_cnt 贷款类App安装数量
    """
    return utils.cal_rate(cnt=app_loan_inst_10Wp_cnt, total=app_loan_inst_cnt, decimals=2)


def cal_app_loan_inst_10K_cnt(app_loan_list_df):
    """
    app_loan_inst_10K_cnt GP下载小于10K的借款类App的个数
    :param app_loan_list_df 贷款类App列表
    """
    return app_loan_list_df.loc[
        (app_loan_list_df.get('installs') < 1000) &
        (app_loan_list_df.get('installs') > 0)
        ].get('app_id').count()


def cal_app_loan_inst_10K_rate(app_loan_inst_10K_cnt, app_loan_inst_cnt):
    """
    app_loan_inst_10K_rate GP下载小于10K的借款类App的个数占总借贷App比例
    :param app_loan_inst_10K_cnt GP下载小于10K的借款类App的个数
    :param app_loan_inst_cnt 贷款类App安装数量
    """
    return utils.cal_rate(cnt=app_loan_inst_10K_cnt, total=app_loan_inst_cnt, decimals=2)


def cal_app_loan_inst_5Wm_cnt(app_loan_list_df):
    """
    app_loan_inst_5Wm_cnt GP下载小于（含）5W的借款类App的个数
    :param app_loan_list_df 贷款类App列表
    """
    return app_loan_list_df.loc[
        (app_loan_list_df.get('installs') <= 50000) &
        (app_loan_list_df.get('installs') > 0)
        ].get('app_id').count()


def cal_app_loan_inst_5Wm_rate(app_loan_inst_5Wm_cnt, app_loan_inst_cnt):
    """
    app_loan_inst_5Wm_rate GP下载小于（含）5W的借款类App的个数占总借贷App比例
    :param app_loan_inst_5Wm_cnt GP下载小于（含）5W的借款类App的个数
    :param app_loan_inst_cnt 贷款类App安装数量
    """
    return utils.cal_rate(cnt=app_loan_inst_5Wm_cnt, total=app_loan_inst_cnt, decimals=2)


def cal_app_loan_inst_10Wm_cnt(app_loan_list_df):
    """
    app_loan_inst_10Wm_cnt GP下载小于（含）10W的借款类App的个数
    :param app_loan_list_df 贷款类App列表
    """
    return app_loan_list_df.loc[
        (app_loan_list_df.get('installs') <= 100000) &
        (app_loan_list_df.get('installs') > 0)
        ].get('app_id').count()


def cal_app_loan_inst_10Wm_rate(app_loan_inst_10Wm_cnt, app_loan_inst_cnt):
    """
    app_loan_inst_10Wm_rate GP下载小于（含）10W的借款类App的个数占总借贷App比例
    :param app_loan_inst_10Wm_cnt GP下载小于（含）10W的借款类App的个数
    :param app_loan_inst_cnt 贷款类App安装数量
    """
    return utils.cal_rate(cnt=app_loan_inst_10Wm_cnt, total=app_loan_inst_cnt, decimals=2)


def cal_app_loan_inst_d0_cnt(app_loan_list_df, apply_date, days=0):
    """
    app_loan_inst_d0_cnt 0天内下载的借款类App的个数
    :param app_loan_list_df 贷款类App列表
    :param apply_date 订单申请时间
    :param days 间隔天数
    """
    if days == 0:
        start_date = pd.to_datetime(apply_date.date())
    else:
        start_date = apply_date - pd.Timedelta(days=days)

    # print(start_date, end_date)
    return app_loan_list_df.loc[
        # 下载时间在当天
        (app_loan_list_df.get('firstInstallTime') >= start_date) &
        (app_loan_list_df.get('firstInstallTime') < apply_date)
        ].get('app_id').count()


def cal_app_loan_inst_d0_rate(app_loan_inst_d0_cnt, app_loan_inst_cnt):
    """
    app_loan_inst_d0_rate 0天内下载的借款类App占总借款App的比例
    :param app_loan_inst_d0_cnt 0天内下载的借款类App的个数
    :param app_loan_inst_cnt 贷款类App安装数量
    """
    return utils.cal_rate(cnt=app_loan_inst_d0_cnt, total=app_loan_inst_cnt, decimals=2)


def cal_app_loan_inst_d7_cnt(app_loan_list_df, apply_date):
    """
    app_loan_inst_d0_cnt 7天内下载的借款类App的个数
    :param app_loan_list_df 贷款类App列表
    :param apply_date 订单申请时间
    """
    return cal_app_loan_inst_d0_cnt(app_loan_list_df=app_loan_list_df, apply_date=apply_date, days=7)


def cal_app_loan_inst_d7_rate(app_loan_inst_d7_cnt, app_loan_inst_cnt):
    """
    app_loan_inst_d7_rate 7天内下载的借款类App占总借款App的比例
    :param app_loan_inst_d7_cnt 7天内下载的借款类App的个数
    :param app_loan_inst_cnt 贷款类App安装数量
    """
    return utils.cal_rate(cnt=app_loan_inst_d7_cnt, total=app_loan_inst_cnt, decimals=2)


def cal_app_loan_install_day_list(app_loan_list_df, apply_date):
    """
    app_install_days 借贷App的安装间隔天数
    :param app_loan_list_df 贷款类App列表
    :param apply_date 订单申请时间
    """
    df = app_loan_list_df.dropna(subset=['firstInstallTime'])
    df = (apply_date - df.get('firstInstallTime')).dt.days
    return df.sort_values().reset_index(drop=True)


def cal_app_loan_maxDays(app_loan_install_days_list_df):
    """
    app_loan_maxDays 借贷App的安装天数最大值
    :param app_loan_install_days_list_df 借贷App的安装间隔天数列表
    """
    return app_loan_install_days_list_df.max() if len(app_loan_install_days_list_df) > 0 else -9999


def cal_app_loan_medianDays(app_loan_install_days_list_df):
    """
    app_loan_medianDays 借贷App的安装天数中位数
    :param app_loan_install_days_list_df 借贷App的安装间隔天数列表
    """
    length = len(app_loan_install_days_list_df)
    index = np.floor(length / 2)
    if index >= length - 1:
        index = np.ceil(length / 2)
    if length > index:
        return app_loan_install_days_list_df.loc[index]
    return 0


def cal_app_riskLoanAppCnt(app_risk_list_df):
    """
    app_riskLoanAppCnt 高危借款类App的个数
    :param app_risk_list_df 风险贷款app列表
    """
    return app_risk_list_df.get('PackageName').count()


async def cal_app_features(app_df, apply_date):
    """
    计算app特征
    params:app_df app数据
    params:apply_date 订单申请时间
    """
    app_inst_cnt = len(app_df)
    default_features = {
        # 安装的APP个数:app_inst_cnt
        'app_inst_cnt': app_inst_cnt,
        # 3天内安装的APP个数:app_inst_d3_cnt
        'app_inst_d3_cnt': 0,
        # 贷款类App数量:app_loan_inst_cnt
        "app_loan_inst_cnt": 0,
        # 发布3个月以上的借款类App的个数:app_loan_release3mp_cnt
        'app_loan_release3mp_cnt': 0,
        # 发布6个月以上的借款类App的个数:app_loan_release6mp_cnt
        'app_loan_release6mp_cnt': 0,
        # 发布1年以上的借款类App的个数:app_loan_release1yp_cnt
        'app_loan_release1yp_cnt': 0,
        # 发布2个月内的借款类App的个数:app_loan_release2mw_cnt
        'app_loan_release2mw_cnt': 0,
        # 发布2个月内的借款类App占总借贷App比例:app_loan_release2m_rate
        'app_loan_release2m_rate': -9999.0,
        # 发布3个月内的借款类App的个数:app_loan_release3mw_cnt
        'app_loan_release3mw_cnt': 0,
        # 发布3个月内的借款类App占总借贷App比例:app_loan_release3m_rate
        'app_loan_release3m_rate': -9999.0,
        # GP下载大于（含）10W的借款类App的个数:app_loan_inst_10Wp_cnt
        'app_loan_inst_10Wp_cnt': 0,
        # GP下载大于（含）10W的借款类App的个数占总借贷App比例:app_loan_inst_10Wp_rate
        'app_loan_inst_10Wp_rate': -9999.0,
        # GP下载小于10K的借款类App的个数:app_loan_inst_10K_cnt
        'app_loan_inst_10K_cnt': 0,
        # GP下载小于10K的借款类App的个数占总借贷App比例:app_loan_inst_10K_rate
        'app_loan_inst_10K_rate': -9999.0,
        # GP下载小于（含）5W的借款类App的个数:app_loan_inst_5Wm_cnt
        'app_loan_inst_5Wm_cnt': 0,
        # GP下载小于（含）5W的借款类App的个数占总借贷App比例:app_loan_inst_5Wm_rate
        'app_loan_inst_5Wm_rate': -9999.0,
        # GP下载小于（含）10W的借款类App的个数:app_loan_inst_10Wm_cnt
        'app_loan_inst_10Wm_cnt': 0,
        # GP下载小于（含）10W的借款类App的个数占总借贷App比例:app_loan_inst_10Wm_rate
        'app_loan_inst_10Wm_rate': -9999.0,
        # 0天内下载的借款类App的个数:app_loan_inst_d0_cnt
        'app_loan_inst_d0_cnt': 0,
        # 0天内下载的借款类App占总借款App的比例:app_loan_inst_d0_rate
        'app_loan_inst_d0_rate': -9999.0,
        # 7天内下载的借款类App的个数:app_loan_inst_d7_cnt
        'app_loan_inst_d7_cnt': 0,
        # 7天内下载的借款类App占总借款App的比例:app_loan_inst_d7_rate
        'app_loan_inst_d7_rate': -9999.0,
        # 借贷App的安装天数最大值:app_loan_maxDays
        'app_loan_maxDays': 0,
        # 借贷App的安装天数中位数:app_loan_medianDays
        'app_loan_medianDays': 0,
        # 高危借款类App的个数:app_riskLoanAppCnt
        'app_riskLoanAppCnt': 0,
    }
    
    if app_inst_cnt == 0:
        return default_features

    # 如果下列语句报错, 做一个异常处理, 抛出default_features
    try:
        apply_date = pd.to_datetime(apply_date) if apply_date is not None else pd.Timestamp.now()
        # 获取贷款类App数据
        loan_app_df = utils_json.get_loan_app_data()
        app_loan_list_df = cal_app_loan_list(app_df=app_df, loan_app_df=loan_app_df)
        # 获取风险类App数据
        risk_app_df = utils_json.get_risk_app_data()
        app_risk_list_df = cal_app_risk_list(app_df=app_df, risk_app_df=risk_app_df)
        # 借贷App的安装间隔天数列表
        app_loan_install_day_list_df = cal_app_loan_install_day_list(
            app_loan_list_df=app_loan_list_df, apply_date=apply_date
        )
        # 贷款类App数量:app_loan_inst_cnt
        app_loan_inst_cnt = cal_app_loan_inst_cnt(app_loan_list_df=app_loan_list_df)
        # 发布2个月内的借款类App的个数:app_loan_release2mw_cnt
        app_loan_release2mw_cnt = cal_app_loan_release2mw_cnt(
            app_loan_list_df=app_loan_list_df, apply_date=apply_date
        )
        # 发布3个月内的借款类App的个数:app_loan_release3mw_cnt
        app_loan_release3mw_cnt = cal_app_loan_release3mw_cnt(
            app_loan_list_df=app_loan_list_df, apply_date=apply_date
        )
        # GP下载大于（含）10W的借款类App的个数:app_loan_inst_10Wp_cnt
        app_loan_inst_10Wp_cnt = cal_app_loan_inst_10Wp_cnt(app_loan_list_df=app_loan_list_df)
        # GP下载小于10K的借款类App的个数:app_loan_inst_10K_cnt
        app_loan_inst_10K_cnt = cal_app_loan_inst_10K_cnt(app_loan_list_df=app_loan_list_df)
        # GP下载小于（含）5W的借款类App的个数:app_loan_inst_5Wm_cnt
        app_loan_inst_5Wm_cnt = cal_app_loan_inst_5Wm_cnt(app_loan_list_df=app_loan_list_df)
        # GP下载小于（含）10W的借款类App的个数:app_loan_inst_10Wm_cnt
        app_loan_inst_10Wm_cnt = cal_app_loan_inst_10Wm_cnt(app_loan_list_df=app_loan_list_df)
        # 0天内下载的借款类App的个数:app_loan_inst_d0_cnt
        app_loan_inst_d0_cnt = cal_app_loan_inst_d0_cnt(
            app_loan_list_df=app_loan_list_df, apply_date=apply_date
        )
        # 7天内下载的借款类App的个数:app_loan_inst_d7_cnt
        app_loan_inst_d7_cnt = cal_app_loan_inst_d7_cnt(
            app_loan_list_df=app_loan_list_df, apply_date=apply_date
        )
    except:
        # 以上代码出故障, 返回默认值
        return default_features

    return {
        # 安装的APP个数:app_inst_cnt
        'app_inst_cnt': app_inst_cnt,
        # 3天内安装的APP个数:app_inst_d3_cnt
        'app_inst_d3_cnt': cal_app_inst_d3_cnt(app_df=app_df, apply_date=apply_date),
        # 贷款类App数量:app_loan_inst_cnt
        "app_loan_inst_cnt": app_loan_inst_cnt,
        # 发布3个月以上的借款类App的个数:app_loan_release3mp_cnt
        'app_loan_release3mp_cnt': cal_app_loan_release3mp_cnt(
            app_loan_list_df=app_loan_list_df, apply_date=apply_date
        ),
        # 发布6个月以上的借款类App的个数:app_loan_release6mp_cnt
        'app_loan_release6mp_cnt': cal_app_loan_release6mp_cnt(
            app_loan_list_df=app_loan_list_df, apply_date=apply_date
        ),
        # 发布1年以上的借款类App的个数:app_loan_release1yp_cnt
        'app_loan_release1yp_cnt': cal_app_loan_release1yp_cnt(
            app_loan_list_df=app_loan_list_df, apply_date=apply_date
        ),
        # 发布2个月内的借款类App的个数:app_loan_release2mw_cnt
        'app_loan_release2mw_cnt': app_loan_release2mw_cnt,
        # 发布2个月内的借款类App占总借贷App比例:app_loan_release2m_rate
        'app_loan_release2m_rate': cal_app_loan_release2m_rate(
            app_loan_release2mw_cnt=app_loan_release2mw_cnt, app_loan_inst_cnt=app_loan_inst_cnt
        ),
        # 发布3个月内的借款类App的个数:app_loan_release3mw_cnt
        'app_loan_release3mw_cnt': app_loan_release3mw_cnt,
        # 发布3个月内的借款类App占总借贷App比例:app_loan_release3m_rate
        'app_loan_release3m_rate': cal_app_loan_release3m_rate(
            app_loan_release3mw_cnt=app_loan_release3mw_cnt, app_loan_inst_cnt=app_loan_inst_cnt
        ),
        # GP下载大于（含）10W的借款类App的个数:app_loan_inst_10Wp_cnt
        'app_loan_inst_10Wp_cnt': app_loan_inst_10Wp_cnt,
        # GP下载大于（含）10W的借款类App的个数占总借贷App比例:app_loan_inst_10Wp_rate
        'app_loan_inst_10Wp_rate': cal_app_loan_inst_10Wp_rate(
            app_loan_inst_10Wp_cnt=app_loan_inst_10Wp_cnt, app_loan_inst_cnt=app_loan_inst_cnt
        ),
        # GP下载小于10K的借款类App的个数:app_loan_inst_10K_cnt
        'app_loan_inst_10K_cnt': app_loan_inst_10K_cnt,
        # GP下载小于10K的借款类App的个数占总借贷App比例:app_loan_inst_10K_rate
        'app_loan_inst_10K_rate': cal_app_loan_inst_10K_rate(
            app_loan_inst_10K_cnt=app_loan_inst_10K_cnt, app_loan_inst_cnt=app_loan_inst_cnt
        ),
        # GP下载小于（含）5W的借款类App的个数:app_loan_inst_5Wm_cnt
        'app_loan_inst_5Wm_cnt': cal_app_loan_inst_5Wm_cnt(app_loan_list_df=app_loan_list_df),
        # GP下载小于（含）5W的借款类App的个数占总借贷App比例:app_loan_inst_5Wm_rate
        'app_loan_inst_5Wm_rate': cal_app_loan_inst_5Wm_rate(
            app_loan_inst_5Wm_cnt=app_loan_inst_5Wm_cnt, app_loan_inst_cnt=app_loan_inst_cnt
        ),
        # GP下载小于（含）10W的借款类App的个数:app_loan_inst_10Wm_cnt
        'app_loan_inst_10Wm_cnt': app_loan_inst_10Wm_cnt,
        # GP下载小于（含）10W的借款类App的个数占总借贷App比例:app_loan_inst_10Wm_rate
        'app_loan_inst_10Wm_rate': cal_app_loan_inst_10Wm_rate(
            app_loan_inst_10Wm_cnt=app_loan_inst_10Wm_cnt, app_loan_inst_cnt=app_loan_inst_cnt
        ),
        # 0天内下载的借款类App的个数:app_loan_inst_d0_cnt
        'app_loan_inst_d0_cnt': app_loan_inst_d0_cnt,
        # 0天内下载的借款类App占总借款App的比例:app_loan_inst_d0_rate
        'app_loan_inst_d0_rate': cal_app_loan_inst_d0_rate(
            app_loan_inst_d0_cnt=app_loan_inst_d0_cnt, app_loan_inst_cnt=app_loan_inst_cnt
        ),
        # 7天内下载的借款类App的个数:app_loan_inst_d7_cnt
        'app_loan_inst_d7_cnt': app_loan_inst_d7_cnt,
        # 7天内下载的借款类App占总借款App的比例:app_loan_inst_d7_rate
        'app_loan_inst_d7_rate': cal_app_loan_inst_d7_rate(
            app_loan_inst_d7_cnt=app_loan_inst_d7_cnt, app_loan_inst_cnt=app_loan_inst_cnt
        ),
        # 借贷App的安装天数列表
        # 'app_loan_install_day_list': app_loan_install_day_list_df.to_dict(),
        # 借贷App的安装天数最大值:app_loan_maxDays
        'app_loan_maxDays': cal_app_loan_maxDays(app_loan_install_days_list_df=app_loan_install_day_list_df),
        # 借贷App的安装天数中位数:app_loan_medianDays
        'app_loan_medianDays': cal_app_loan_medianDays(app_loan_install_days_list_df=app_loan_install_day_list_df),
        # 高危借款类App的个数:app_riskLoanAppCnt
        'app_riskLoanAppCnt': cal_app_riskLoanAppCnt(app_risk_list_df=app_risk_list_df),
    }


if __name__ == '__main__':
    # 记录程序开始时间
    start_time = time.time()

    apply_no = '167834013385583282'
    # apply_no = '170936262878735483'
    # apply_no = '171090181602962693'
    # apply_no = '167446013851750028'
    # apply_no = '169225581607074281'
    # apply_no = '171075064842338023'
    # 订单数据
    order_dict = utils_sql.get_mysql_order(apply_no)
    print("order_dict 程序运行时长：", time.time() - start_time, "秒")

    # 根据订单获取json文件路径
    json_file_url = utils_sql.get_mysql_json_url(apply_no)
    # 远程获取JSON数据
    json_string = utils.load_remote_json(json_file_url)
    # 将字符串转成json对象
    json_dict = utils.parse_json(json_string)
    print("json_dict 程序运行时长：", time.time() - start_time, "秒")

    # 读取App数据
    app_string = json_dict.get('apps') if 'apps' in json_dict.keys() else None
    app_df = pd.read_json(app_string) if utils.is_json_string(app_string) else None

    # app 相关参数计算
    app_features = asyncio.run(cal_app_features(
        app_df=app_df, apply_date=order_dict.get('apply_date')
    ))
    print("app_features 程序运行时长：", time.time() - start_time, "秒")
    print(app_features)
